<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类目名称" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入类目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择品牌" prop="brandId">
        <el-select
          v-model="queryParams.brandId"
          placeholder="请选择"
          clearable>
          <el-option
            v-for="dict in brandList"
            :key="dict.value"
            :label="dict.lable"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="collectProductCategoryList"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="类目名称" prop="name" />
      <el-table-column label="商品个数" prop="proNumber" align="center" />
      <el-table-column label="品牌" prop="brand" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.brand">
            <el-tag style="margin: 3px;" v-for="(val, key, i) in scope.row.brand" :key="i">{{ key }}：{{ val }}</el-tag>
          </div>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { listCollectProductCategory } from "@/api/gather/brandCategory";
import { getBrand } from "@/api/gather/collectProduct";


export default {
  name: "BrandCategory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 采集商品类目表格数据
      collectProductCategoryList: [],
      // 查询参数
      queryParams: {
        categoryName: undefined,
        brandId: undefined
      },
      // 品牌列表
      brandList: []
    };
  },
  created() {
    this.getBrandList();
    this.getList();
  },
  methods: {
    /** 查询采集商品类目列表 */
    getList() {
      this.loading = true;
      listCollectProductCategory(this.queryParams).then(response => {
        this.collectProductCategoryList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取品牌选择列表
    getBrandList() {
      getBrand().then(res => {
        this.brandList = res.data
      });
    }
  }
};
</script>
